package view.panel
{
	import control.panel.PlayPanelControl;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import global.GameState;
	import global.Resource;
	
	import utils.ColorTool;
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.card.ICard;
	import view.components.ImgButton;
	
	public class PlayPanel extends AbsPanel
	{
		private var lastcards:Array;
		private var cardContainer:Sprite;
		private var systemText:*;
		private var playText:*;
		private var playPanelControl:PlayPanelControl;
		private var frame:DisplayObject;
		private var frameHint:DisplayObject; 
		private var frameHintAuto:DisplayObject;
		
		public var inputText:TextField;
		
		public var playBottomHint:PlayBottomHint;
		
		private var playBut:ImgButton;
		private var passBut:ImgButton;
		private var hintBut:ImgButton;
		public var autoBut:ImgButton;
		public var autoCancelBut:ImgButton;
		
		private var increment:int;
		private var setter:int = 1;
		
		//名言名句
		private var famusRemark:FamusRemark;
		
		public function PlayPanel(panelControl:PlayPanelControl)
		{
			super(panelControl);
			playPanelControl = panelControl;
			init();
		}
		
		private function init():void
		{
			initSystemMessage();
			initPlayMessage();
			resetBg(780,112);
			initFrame();
			initButs();
			initCardContainer();
			initInput();
			initPlayBottomHint();
			hideAllBut(false);
		}
		
		
		private function initPlayBottomHint():void
		{
			playBottomHint = new PlayBottomHint();
			playBottomHint.x = 270;
			playBottomHint.y = 88;
			addChild(playBottomHint);
		}
		
		private function initInput():void
		{
			inputText = TextTool.getTextField(140,23,12,0xFFFFFF,true);
			inputText.x = 585;
			inputText.y = 8;
			inputText.useRichTextClipboard = true;
			inputText.wordWrap = false;
			inputText.type = TextFieldType.INPUT;
			inputText.maxChars = 100;
			
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(1,0xFFFFFF,.6);
			shape.graphics.beginFill(0,1);
			shape.graphics.drawRoundRect(0,0,142,18,10,10);
			shape.graphics.endFill();
			shape.x = 585;
			shape.y = 8;
			addChild(shape);
			addChild(inputText);
		}
		
		private function initFrame():void
		{
			frame = Resource.getImg("face_frame");
			frame.x = 268;
			frame.y = 0;
			
			addChild(frame);
			
			frameHint = Resource.getImg("face_turnSign");
			frameHint.x = frame.x + (frame.width - frameHint.width) * .5;
			frameHint.y = - 20;
			
			frameHint.visible = false;
			
			addChild(frameHint);
			
			frameHintAuto = Resource.getImg("face_autoSign");
			frameHintAuto.x = frame.x + (frame.width - frameHintAuto.width) * .5;
			frameHintAuto.y = -20;
			frameHintAuto.visible = false;
			
			addChild(frameHintAuto);
			
			frameHintAuto.addEventListener(Event.COMPLETE,onFrameComplete);
		}
		
		private function onFrameComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onFrameComplete);
			frameHintAuto.x = frame.x + (frame.width - frameHintAuto.width) * .5;
			frameHintAuto.y = -35;
			
			frameHint.x = frame.x + (frame.width - frameHint.width) * .5;
			frameHint.y = - 35;
		}
		
		public function hintFrame():void
		{
			frame.alpha = 0.2 * increment;
			
			increment += setter;	
			
			if (increment >= 5)
			{
				setter = -1;
				frameHint.y = - 40;
			}
			
			if (increment <= 0)
			{
				setter = 1;
				frameHint.y = - 35;
			}
		}
		
		public function set  yellowFrame(v:Boolean):void
		{
			frameHint.y = -30;
			if (v)
			{
				ColorTool.yellow(frame);
			}
			else
			{
				ColorTool.saturation(frame);
				
			}
			
		}
		
		
		
		
		private function initCardContainer():void
		{
			cardContainer = new Sprite();
			cardContainer.x = 277;
			cardContainer.y = 2;
			addChild(cardContainer);
		}
		
		public function enableCardContainer():void
		{
			if (GameState.isAutoPlay)
			{
				_view.playPanel.autoBut.visible = false;
				_view.playPanel.autoCancelBut.visible = true;
				cardContainer.mouseEnabled = false;
				cardContainer.mouseChildren = false;
			}
			else
			{
				_view.playPanel.autoBut.visible = true;
				_view.playPanel.autoCancelBut.visible = false;
				cardContainer.mouseEnabled = true;
				cardContainer.mouseChildren = true;
			}
		}
		
		private function initButs():void
		{
			playBut = new ImgButton(415,23,0,onButClick,55,30,"butBg",null,"出牌");
			passBut = new ImgButton(475,23,1,onButClick,55,30,"butBg",null,"过牌");
			hintBut = new ImgButton(415,55,2,onButClick,55,22,"butBg",null,"提示");
			autoBut = new ImgButton(475,55,3,onButClick,55,22,"butBg",null,"托管");
			autoCancelBut = new ImgButton(475,55,3,onButClick,55,22,"butBg",null,"取消托管");
			addChild(autoCancelBut);
			autoCancelBut.visible = false;
			addChild(playBut);
			addChild(passBut);
			addChild(hintBut);
			addChild(autoBut);
			
			var but:ImgButton = new ImgButton(538,6,4,onButClick,22,22,"butBg","panel_but_emotion");
			but.isUpInfo = true;
			but.info = "表情";
			addChild(but);
			
			but = new ImgButton(560,6,5,onButClick,22,22,"butBg","panel_but_forbiden");
			but.isUpInfo = true;
			but.info = "禁止发言";
			but.mouseEnabled = false;
			addChild(but);
			
			but = new ImgButton(730,6,6,onButClick,22,22,"butBg","panel_but_list");
			but.isUpInfo = true;
			but.info = "快速回复";
			addChild(but);
			
			but = new ImgButton(752,6,7,onButClick,22,22,"butBg","panel_but_send");
			but.isUpInfo = true;
			but.info = "发送";
			addChild(but);
		}
		
		public function switchToReplayState():void
		{
			playBut.mouseEnabled = passBut.mouseEnabled = hintBut.mouseEnabled = autoBut.mouseEnabled =  true;
			frameHint.visible = true;
			frameHintAuto.visible = false;
			
		}
		
		public function switchToPlayState():void
		{
			hintBut.mouseEnabled = passBut.mouseEnabled = false;
			playBut.mouseEnabled = autoBut.mouseEnabled = true;
			frameHint.visible = true;
			frameHintAuto.visible = false;
			
		}
		
		public function switchToWait():void
		{
			
			playBut.mouseEnabled = passBut.mouseEnabled = hintBut.mouseEnabled = false;
			autoBut.mouseEnabled = true;
			
			
			frameHint.visible = false;
			
			if (GameState.isAutoPlay)
			{
				frameHintAuto.visible = true;
			}
			else
			{
				frameHintAuto.visible = false;
			}
		}
		
		public function hideAllBut(v:Boolean):void
		{
			playBut.visible = passBut.visible = hintBut.visible = autoBut.visible = autoCancelBut.visible = v;
			frame.visible = v;
			frameHint.visible = v;
			
			
		}
		
		private function onButClick(event:MouseEvent):void
		{
			switch (event.target.id)
			{
				case 0:
					playPanelControl.onPlay();
					break;
				case 1:
					playPanelControl.onPass();
					break;
				case 2:	
					playPanelControl.onHint();
					break;
				case 3://托管
					playPanelControl.onAutoBut();
					break;
				case 4:
					playPanelControl.onEmote();
					break;
				case 6:
					playPanelControl.onFastReply();
					break;
				case 7:
					playPanelControl.onSendChat();
					break;
			}
		}
		
		public function showCardContainerAlpha(alpha:Number):void
		{
			//cardContainer.alpha = alpha;
		}
		
		private function initPlayMessage():void
		{
			playText = TextTool.getTextArea(236,75);
			playText.x = 775 - playText.width;
			playText.y = 32;
			addChild(playText);
		}
		
		private function initSystemMessage():void
		{
			if (GameState.traceType == 3)
			{
				systemText = TextTool.getTextArea(336,300);
				systemText.x = 5;
				systemText.y = 6;
			}
			else
			{
				systemText = TextTool.getTextArea(250,100);
				systemText.x = 5;
				systemText.y = 6;
			}
			addChild(systemText);
		}
		
		public function updateChatMsg(msg:String):void
		{
			playText.htmlText += "<font size='12' color='#FFFFFF'>" + msg + "</font>";
			playText.verticalScrollPosition = playText.maxVerticalScrollPosition;
		}
		
		public function updateServerMsg(msg:String):void
		{
			systemText.htmlText +="<font size='12' color='#FFFFFF'>" + msg + "</font>";
			systemText.verticalScrollPosition = systemText.maxVerticalScrollPosition;
		}
		
		public function updateBottomHint(content:String):void
		{
			playBottomHint.hint(content);
		}
		
		public function layoutCards(cards:Array):void
		{
			var card:ICard;
			for (var i:int=0; i<cards.length; i++)
			{
				card = cards[i];
				card.self.x = i * 15;
				card.self.y = 20;
				addCardListener(card);
				cardContainer.addChild(card.self);
			}	
			lastcards = cards;
		}
		
		private function addCardListener(card:ICard):void
		{
			if (card)
			{
				card.addEventListener(MouseEvent.CLICK,onCardClick);
			}
		}
		
		private function onCardClick(event:MouseEvent):void
		{
			(panelControl as PlayPanelControl).onCardClick(event.target.id);
		}
		
		public function clearLayourCards():void
		{
			if (lastcards == null)
			{
				return;
			}
			
			var card:ICard;
			for (var n:String in lastcards)
			{
				
				card = lastcards[n];
				card.removeEventListener(MouseEvent.CLICK,onCardClick);
				card.destroy();
				DisplayTool.removeChild(cardContainer,card.self);
				
			}
			lastcards = null;
			
			
			
			if (!GameState.isRoundStarted)
			{
				frameHint.visible = false;
				frameHintAuto.visible = false;
			}
		}
		
		public function clear():void
		{
			clearLayourCards();
			systemText.htmlText = "";
			playText.htmlText = "";

		}
	}
}